<script>
$(function(){
	$(".publish").bind("click",function(){
		html = editor.html();

			// 同步数据后可以直接取得textarea的value
			editor.sync();
			//html = document.getElementById('editor_id').value; // 原生API
			///html = K('#editor_id').val(); // KindEditor Node API
			html = $('.pubwork').val(); // jQuery
			if(html == ""){
				$.globalMessenger().post("日志内容不能为空");
				return;
			}
		$.post("__APP__/Work/worklogPublish",{title:getInputValueByName('title'),work:html},function(json){
			//alert($(this).parents(".modal").length);
			$("#work").modal("hide");
			$.globalMessenger().post(json.info);
			if($(".panel").length == 0){
				MP('__APP__/Work/workLog','work-log');
			}else{
				var per_worklog = "<div class='panel panel-default' style='margin-top:10px;display:none'><div class='panel-heading'><h4 class='panel-title'>"+getInputValueByName('title')+"<span class='pull-right'>"+json.data.pulisher+"&nbsp;&nbsp;&nbsp;"+json.data.create_time+"</span></h4></div><div id='collapseOne' class='panel-collapse collapse in'><div class='panel-body' style='word-break: break-all;'>"+html+"</div><div class='work-log-content{$vo.worklog_id} hide'>"+html+"</div></div></div>";
				$("#accordion").prepend(per_worklog).children(":first").slideDown(500);
			}
			$("input[name='title']").val("");
			html = editor.html("");
		});
	});
	$(".view").bind("click",function(){
		MP('__APP__/Work/workLog','work-log','true');
	});
	$(".home").bind("click",function(){
		$(".content").empty();
	});
	$(".leaveword").bind("click",function(){
		html = editor1.html();
		$.post("__APP__/Comment/reply",{worklog_id:$(this).attr("id"),comment:html},function(json){
			html = editor1.html("");
			if(json.status == 1){
				var insertComment = "<div class='panel panel-default comment-list' style='display:none'><div class='panel-heading'><h4 class='panel-title'><a class='accordion-toggle' data-parent='#accordion'>"+json.data.replier+"&nbsp;&nbsp;&nbsp;"+json.data.create_time+"</a></h4></div><div id='collapseOne' class='panel-collapse collapse in'><div class='panel-body' style='word-break: break-all;'>"+json.data.comment+"</div></div></div>";
				$(".comment-lists").append(insertComment).children(":last").slideDown(500);
			}
			$.globalMessenger().post(json.info);
		});
	});
	$(".tooltips").tooltip();
	$("#feedback").bind("click",function(){
		$("#dialogfeedback").modal("show");
	});
	$(".open-publish").bind("click",function(){
		$("#work").modal("show");
	});
	$(".feedback").bind("click",function(){
		html = editor2.html();
		$.post('__APP__/Public/feedback',{title:$("input[name='feedback']").val(),content:html},function(json){
			if(json.status == 1){	
				html = editor1.html("");
				$("#dialogfeedback").modal("hide");
			}
			$.globalMessenger().post(json.info);
		});
	});
	$(".exit").click(function(){
		$("#system .modal-body").html("是否确认退出系统？");
		$("#system .sure").unbind('click').bind('click',function(){
			logout();
			$("#system").modal("hide");
		});
		$("#system").modal("show");
	});
	$(".personal").click(function(){
		$.post('__APP__/Account/userProfile',function(json){	
			if(json.status == 1){
				pushInputValueByName("name",json.data.name);
				if(json.data.gender == "男"){
					$("#man").attr("checked",true);
				}else{
					$("#womenman").attr("checked",true);
				}
				pushInputValueByName("department-name",json.data.department_name);
				pushInputValueByName("mobile",json.data.mobile);
				pushInputValueByName("email",json.data.email);
				$("textarea[name='remark']").val(json.data.remark);
			}
			$("#personal").modal('show');
		});
	});
	$(".department-manage").die().live('click',function(){
		renameSure("确定");
		var department_id = $(this).attr('department-id');
		$(".operate-title").html("部门管理>>"+$(this).attr("department-name"));
		$(".operate-body").load("__APP__/Department/departmentManage",{department_id:department_id},function(){
		});
		$("#operate").modal('show');
	});
	$(".peruser-manage").die().live('click',function(){
		renameSure("确定");
		var titlename = $(this).attr("user-name");
		var uid = $(this).attr("user-uid");
		if(titlename == ""){
			titlename = "账号:"+$(this).attr("user-account");
		}
		$(".operate-title").html("员工管理>>"+titlename);
		$(".operate-body").load('__APP__/Account/userMsgInfo',{uid:uid},function(){
			$(".operate-sure").unbind('click').click(function(){
				if($("#user-msg-info-form input[name='name']").val() == ""){
					$.globalMessenger().post("姓名不能为空");
					return;
				}
				$.post('__APP__/Account/userProfile/uid/'+uid,$("#user-msg-info-form").serialize(),function(json){
					$.globalMessenger().post(json.info);
				})
			});
		});
		$("#operate").modal('show');
	});
	$(".worker-transfer").die().live('click',function(){
		$(".move-department").remove();
		var user_name = $(this).attr("user-name"),
			user_id = $(this).attr("user-uid"),
			option = "";
		var moveDepartment = "<tr class='move-department'><td colspan='4'><h4 style='text-align:center'>员工<span class='label label-info'>"+user_name+"</span>将被调动到部门<select id='move_id' name='department_id'></select>&nbsp;<input class='btn btn-default close-tranfer' type='button' value='关闭'>&nbsp;<input class='btn btn-primary tranfer-operate' id='"+user_id+"' type='button' value='确定'></h4></td></tr>";
		$.post('__APP__/Department/getDepartment',function(json){
			for(var i = 0;i < json.data.length;i++){
				option  += "<option value='"+json.data[i].department_id+"'>"+json.data[i].department_name+"</option>";
			}
			$("#move_id").append(option);
		});
		$(this).parents("tr").after(moveDepartment);
	});
	$(".tranfer-operate").die().live('click',function(){
		$.post('__APP__/Department/departmentTranfer',{department_id:$("#move_id").val(),user_uid:$(this).attr("id")},function(json){
			$.globalMessenger().post(json.info);
			if(json.status == 1){
				$(".move-department").prev().remove();
				$(".move-department").remove();
			}
		});
	});
	$(".close-tranfer").die().live('click',function(){
		$(this).parents(".move-department").remove();
	});
	$("#select_viewlog_method").die().live('change',function(){
		$("#datepicker").val("");
		$("#search-by-datepicker").attr("view-method",$(this).val());
		searchDepartmentLog($('#select_departmentlog_id').val(),$(this).val(),'__APP__/Department/departmentLogs');
	});
	$("#select_departmentlog_id").die().live('change',function(){
		$("#datepicker").val("");
		$("#search-by-datepicker").attr("department-id",$(this).val());
		searchDepartmentLog($(this).val(),$('#select_viewlog_method').val(),'__APP__/Department/departmentLogs');
	});
	$("#search-by-datepicker").die().live('click',function(){
		if(getInputValueById("datepicker") == ""){
			$.globalMessenger().post("日期为空");
			return;
		}
		var date = getInputValueById("datepicker");
		$(".worker-worklog-list").load("__APP__/Department/departmentLogs .worker-worklog-contents",{department:$(this).attr("department-id"),viewmethod:$(this).attr("view-method"),date:date});
	});
	$(".add-worker").die().live('click',function(){
		addWorker('__APP__/Account/addWorker','#add-account-form','__APP__/Public/reg','__APP__/Account/userProfile');
	});
	$(".worker-leave").die().live('click',function(){
		$("#system .modal-body").html("<h4 style='text-align:center'>确定辞退员工<span class='label label-danger'>"+$(this).attr("user-name")+"</span>吗？</h4>");
		var uid = $(this).attr("user-uid");
		$(this).addClass("selected");
		$("#system .sure").click(function(){
			workerResign('__APP__/Account/workerResign',uid);
		});
		$("#system").modal('show');
	});
	$(".add-customer").die().live('click',function(){
		renameTitle("添加客户");
		bodyLoad('__APP__/Customer/addCustomer',function(){
			rebindSure(function(){
				if($("#customer-name").val() == ""){
					$.globalMessenger().post("客户姓名不能为空");
					return;
				}
				$.post('__APP__/Customer/addCustomer/type/addCustomer',$("#add-customer-form").serialize(),function(json){
					$.globalMessenger().post(json.info);
					if(json.status == 1){
						MP('__APP__/Customer/customerList','customer-list');
						renameTitle("填写用户基本信息");
						bodyLoad('__APP__/Customer/addCustomer/type/individual',function(){
							$("input[name='customer_id']").val(json.data);
							rebindSure(function(){
								$.post('__APP__/Customer/addCustomer/type/individual/control/1',$("#add-individual-form").serialize(),function(json){
									$.globalMessenger().post(json.info);
									if(json.status == 1){
										$("#operate").modal('hide');
									}
								});
							});
						})
					}
				})
			});
		});
		$("#operate").modal('show');
	});
});
</script>